package com.bbz.spring.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 后置处理器，初始化前后进行处理工作
 */
@Component
public class MyBeanPostProcess implements BeanPostProcessor, ApplicationContextAware {
    public ApplicationContext applicationContext;

    /**
     * 在完成初始化前，调用此方法
     *
     * @param bean     创建的Bean 实例
     * @param beanName 创建的Bean name
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization********" + bean);
        System.out.println("postProcessBeforeInitialization********" + beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization********" + bean);
        System.out.println("postProcessBeforeInitialization********" + beanName);
        return bean;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
